package servlet;

import java.io.*;
import java.net.URL;
import java.net.URLEncoder;

import javax.servlet.*;
import javax.servlet.http.*;

import workflow.ScraperManager;

public class Scraper extends HttpServlet {
	
	int p = 0;
	String[] morons = {"the messenger|the messemger",
			"A40Y-HorX-Troll|martin|science guy|ths sckence guy|biff|the hor|xenon|boz|A|Sweetness",
			"Sweetness"};
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String pointer = request.getParameter("p");
		if(pointer==null||pointer.trim().length()==0)return;
//		try{
//			p = Integer.parseInt(pointer);
//	//		if(p<0||p>morons.length-1)return;
//		}catch(Exception e){
//			return;
//		}
		PrintWriter pw = response.getWriter();
		response.setCharacterEncoding("UTF-8");
		try{
			String address = "http://kazoolist.com/wwgha/search.php?p="+pointer;
			URL url = new URL(address);
			BufferedReader br = new BufferedReader(
					new InputStreamReader(url.openStream()));
			StringBuffer sb = new StringBuffer();
			String s;
			while((s = br.readLine())!=null){
				sb.append(s);
			}
			pw.println(sb.toString());
//			pw.println(new ScraperManager().doIt(morons[p]));
		}catch(Exception e){
			pw.println("bad");
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}

}
